<?php
declare (strict_types=1);

namespace app\admin\model\iot;

use app\BaseModel;

/**
 * 积分任务完成情况
 */
class IntegralTaskComplet extends BaseModel
{

    protected $autoWriteTimestamp =true;
    /**
     * 积分任务列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @param string $field
     * @return array
     */
    public static function getList(array $where, int $page=1, int $limit=100, string $orderby = '', string $field = '*'): array
    {
        $data = self::field($field)
            ->where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }



    /**
     * 写入设备袋子余量
     * @param string $imei
     * @param int $allowance
     * @return bool
     */
    public static function deviceAllowance(string $imei, int $allowance = 0): bool
    {
        //更新设备表
        self::update(['allowance' => $allowance], ['imei' => $imei]);
        return true;
    }

    public static function taskCompletCount(array $where)
    {
        return self::where($where)->count();
    }

    public static function getListGroup(array $where, $group,$field,$orderby='task_id asc')
    {
        $data = self::field($field)
            ->where($where)
            ->group($group)
            ->select()
            ->order($orderby)
            ->toArray();
        return $data;
    }
    public static function getListGroupPage(array $where, $group,$field,$orderby='task_id asc',int $page=1, int $limit=100)
    {
        $data = self::field($field)
            ->where($where)
            ->group($group)
            ->orderRaw($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }

}




